//
//  YDChoiceDateAlertView.m
//  YunDongGym
//
//  Created by lqq on 2018/6/1.
//  Copyright © 2018年 lqq. All rights reserved.
//

#import "YDChoiceDateAlertView.h"

@interface YDChoiceDateAlertView ()
@property (weak, nonatomic) IBOutlet UIView *contentView;
@property (weak, nonatomic) IBOutlet UIDatePicker *datePicker;
@property (weak, nonatomic) IBOutlet UIButton *cancleBtn;
@property (weak, nonatomic) IBOutlet UIButton *confirmBtn;

@property (nonatomic, assign) UIDatePickerMode datePickerMode;
@property (nonatomic, strong) NSDate *date;
@property (nonatomic, strong) NSDate *minDate;
@property (nonatomic, strong) NSDate *maxDate;
@property (nonatomic, copy) void(^confirmBlock)(NSDate *date);
@end

@implementation YDChoiceDateAlertView
+ (instancetype)showWithDate:(NSDate *)date
                     minDate:(NSDate *)minDate
                     maxDate:(NSDate *)maxDate
                comfirmBlock:(void(^)(NSDate *date))confirmBlock
{
    return [YDChoiceDateAlertView showWithDate:date datePickerMode:UIDatePickerModeDateAndTime minDate:minDate maxDate:maxDate comfirmBlock:confirmBlock];
}

+ (instancetype)showWithDate:(NSDate *)date
              datePickerMode:(UIDatePickerMode)datePickerMode
                     minDate:(NSDate *)minDate
                     maxDate:(NSDate *)maxDate
                comfirmBlock:(void(^)(NSDate *date))confirmBlock
{
    YDChoiceDateAlertView *alertView = [[[NSBundle mainBundle]loadNibNamed:NSStringFromClass([YDChoiceDateAlertView class]) owner:nil options:nil] lastObject];
    alertView.date = date;
    alertView.minDate = minDate;
    alertView.maxDate = maxDate;
    alertView.datePickerMode = datePickerMode;
    alertView.confirmBlock = confirmBlock;
    [alertView show];
    return alertView;
}


-(void)awakeFromNib
{
    [super awakeFromNib];
     [_datePicker setLocale:[[NSLocale alloc]initWithLocaleIdentifier:@"zh_Hans_CN"]];
    _cancleBtn.layer.cornerRadius = 2;
    _cancleBtn.layer.masksToBounds = YES;
    [_cancleBtn addBorderWithBorderWidth:kOnePX borderColor:HEXColor(@"#9B9B9B")];
    _confirmBtn.layer.cornerRadius = 2;
    _confirmBtn.layer.masksToBounds = YES;
}

- (void)show
{
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    self.frame = window.bounds;
    [window addSubview:self];
    self.datePicker.minimumDate = _minDate;
    self.datePicker.maximumDate = _maxDate;
    [self.datePicker setDate:_date animated:YES];
    self.datePicker.datePickerMode = _datePickerMode;
    self.backgroundColor = [UIColor clearColor];
    [UIView animateWithDuration:0.3 animations:^{
        self.backgroundColor = [UIColor colorWithWhite:0 alpha:0.3];
    }];
}
- (IBAction)cancleBtnClicked:(id)sender {
    [self removeFromSuperview];
}
- (IBAction)confirmBtnClicked:(id)sender {
    if (self.confirmBlock) {
        self.confirmBlock(_datePicker.date);
    }
    [self removeFromSuperview];
}

@end
